<?php

require_once(__DIR__."/EveChar.php");

class EveAccount extends MyObject {
	protected static $tablename = "eveaccs";
	protected static $pkname = "userid";
	protected static $memkey = "EveAccount:";

	protected $chars;
	protected $filename="/tmp/Characters.xml";
	
	public function __construct($userid=0, $data=0) {
		parent::__construct($userid, $data);
		
		$this->getChars();
	}
	
	protected function getChars($email) {
		$apisite = fopen ("https://api.eveonline.com/account/Characters.xml.aspx?userid=$this->userid&apikey=$this->apikey", 'r');
		$filename = "$this->filename?userid=$this->userid";
		$localfile = fopen ($filename, 'w');
		
		$line = fgets ( $apisite, 10024);
		while ($line) {
			//echo ($line."\n");
			fputs ( $localfile, $line );
			$line = fgets ( $apisite, 10024);
		}	

		$xml = simplexml_load_file($filename);

		$path = "//row";

		if (!$res = $xml->xpath($path)) {
			echo "Loading api info failed";
		} else {
			foreach ($res AS $row) {
				$char = new EveChar($row["characterID"], array(
					"charid" => $row["characterID"],
					"name" => $row["name"],
					"corp" => $row["corporationName"]
					/* todo: add aliance info from eve-api */
				));
			}
	   }
	}
}